package com.renge.eduservice.controller;


import com.renge.commonutils.R;
import com.renge.eduservice.entity.EduChapter;
import com.renge.eduservice.entity.json.ChapterJson;
import com.renge.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-04
 */
@Api(description="章节管理")
@RestController
@RequestMapping("/eduservice/edu-chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;
    @ApiOperation(value = "根据课程id查询章节")
    @GetMapping("{courseId}")
    public R listChapterVideo(@ApiParam(name = "courseId", value = "课程ID", required = true)
                              @PathVariable("courseId")String courseId){
       List<ChapterJson>chapterJsons= eduChapterService.listChapterVideo(courseId);
       if(chapterJsons==null){
           return R.error();
       }
        return R.ok().data("chapterJsons",chapterJsons);
    }
    @ApiOperation(value = "根据章节id查询章节")
    @GetMapping("/chapter/{chapterId}")
    public R listChapterVideobyChapterId(@ApiParam(name = "chapterId", value = "章节ID", required = true)
                              @PathVariable("chapterId")String chapterId){
        EduChapter chapter=eduChapterService.listChapterVideobyChapterId(chapterId);
        return chapter==null?R.error():R.ok().data("chapter",chapter);
    }
    @ApiOperation(value = "添加或者修改章节")
    @PostMapping("/chapter/save")
    public R savaOrUpdateChapterVideo(@ApiParam(name = "eduChapter", value="章节",required = true)
                                      @RequestBody EduChapter eduChapter){
        boolean b = eduChapterService.saveOrUpdate(eduChapter);
        if(!b){
            return R.error();
        }
        return R.ok();
    }
    @ApiOperation(value = "删除章节-判断有小结就不删除")
    @DeleteMapping("/chapter/{chapterId}")
    public R deleteChapterVideo(@ApiParam(name = "chapterId", value="章节id",required = true)
                                    @PathVariable("chapterId") String chapterId){
        boolean b = eduChapterService.deleteChapterVideo(chapterId);
        return b?R.ok():R.error();
    }


}

